package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.MtWeddingHosts;
import com.lu.statistics.entity.enums.ChoiceTypeEnum;
import com.lu.statistics.entity.enums.ProductTypeEnum;
import com.lu.statistics.entity.front.response.ProGroupResponse;
import com.lu.statistics.entity.front.response.WeddingHostsResponse;
import com.lu.statistics.mapper.MtWeddingHostsMapper;
import com.lu.statistics.service.GroupService;
import com.lu.statistics.service.WeddingHostsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WeddingHostsServiceImpl extends ServiceImpl<MtWeddingHostsMapper, MtWeddingHosts> implements WeddingHostsService, GroupService {
    @Autowired
    private MtWeddingHostsMapper hostsMapper;

    @Override
    public IPage<MtWeddingHosts> queryPageHosts(String choiceType, int current, int pageSize) {
        Page<MtWeddingHosts> page = new Page(current, pageSize);
        QueryWrapper<MtWeddingHosts> wrapper = new QueryWrapper<>();
        if (choiceType != null) {
            wrapper.eq("choice_type", choiceType);
        }
        wrapper.orderBy(true, false, "create_time");
        return hostsMapper.selectPage(page, wrapper);
    }

    @Override
    public ProGroupResponse queryGroupList(int pageNo, int pageSize) {
        ProGroupResponse groupResponse = new ProGroupResponse();
        List<WeddingHostsResponse> newList = new ArrayList<>();
        IPage page = queryPageHosts(ChoiceTypeEnum.SINGLE_WEDDING.name(), pageNo, pageSize);
        if (page.getTotal() > 0) {
            for (Object item : page.getRecords()) {
                MtWeddingHosts hosts = (MtWeddingHosts) item;
                WeddingHostsResponse hostsResponse = new WeddingHostsResponse();
                String url = "/api/file/previewPhoto?fileFullPath=" + hosts.getImgUrl();
                BeanUtils.copyProperties(hosts, hostsResponse);
                hostsResponse.setImgUrl(url);
                newList.add(hostsResponse);
            }
            groupResponse.setHostTotal(page.getTotal());
        }
        groupResponse.setHostList(newList);
        return groupResponse;
    }

    @Override
    public ProductTypeEnum getType() {
        return ProductTypeEnum.ORDE;
    }
}
